package cn.lyw.seckillsession.service.service.impl;

import cn.common.beancopy.util.BeanCopierUtil;
import cn.common.restresult.util.RestResult;
import cn.common.restresult.util.ResultEnum;
import cn.hutool.core.lang.generator.ObjectIdGenerator;
import cn.lyw.seckillsession.mapper.mapper.SeckillSessionMapper;
import cn.lyw.seckillsession.mapper.pojo.SmallMiSeckillSession;
import cn.lyw.seckillsession.service.bo.AddSessionBO;
import cn.lyw.seckillsession.service.bo.EditSessionBO;
import cn.lyw.seckillsession.service.domain.GetSessionAllDO;
import cn.lyw.seckillsession.service.domain.GetSessionByIdDO;
import cn.lyw.seckillsession.service.domain.GetSessionTodayDO;
import cn.lyw.seckillsession.service.service.SeckillSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author   lyw
 * @Description 秒杀场次逻辑层
 * @Date 14:14 2021/7/23
 * @Param
 * @return
 */
@Service
public class SeckillSessionServiceImpl implements SeckillSessionService {


    @Autowired
    private SeckillSessionMapper seckillSessionMapper;


    /**
     * @Author   lyw
     * @Description 查询全部场次信息
     * @Date 15:29 2021/7/23
     * @Param
     * @return
     */
    @Override
    public List<GetSessionAllDO> getSessionAll() {

        //查询
        List<SmallMiSeckillSession> smallMiSeckillSessions = seckillSessionMapper.selectAll();

        //转换参数
        List<GetSessionAllDO> getSessionAllDOS = BeanCopierUtil.copyList(smallMiSeckillSessions, GetSessionAllDO.class);

        return getSessionAllDOS;
    }


    /**
     * @Author   lyw
     * @Description 查询单个场次信息
     * @Date 15:47 2021/7/23
     * @Param
     * @return
     */
    @Override
    public GetSessionByIdDO getSessionById(String sessionId) {

        //查询
        SmallMiSeckillSession smallMiSeckillSession = seckillSessionMapper.selectByPrimaryKey(sessionId);

        //转换参数
        GetSessionByIdDO getSessionByIdDO = BeanCopierUtil.copyObj(smallMiSeckillSession, GetSessionByIdDO.class);

        return getSessionByIdDO;
    }


    /**
     * @Author   lyw
     * @Description 查询当天的秒杀场次
     * @Date 12:26 2021/7/24
     * @Param
     * @return
     */
    @Override
    public List<GetSessionTodayDO> getSessionToday() {

        //调用方法
        List<SmallMiSeckillSession> seckillSessionToday = seckillSessionMapper.getSeckillSessionToday();

        //转换返回值
        List<GetSessionTodayDO> getSessionTodayDOS = BeanCopierUtil.copyList(seckillSessionToday, GetSessionTodayDO.class);

        return getSessionTodayDOS;
    }


    /**
     * @Author   lyw
     * @Description 根据id删除场次信息
     * @Date 15:53 2021/7/23
     * @Param
     * @return
     */
    @Override
    public RestResult delSessionById(String sessionId) {

        //删除
        int i = seckillSessionMapper.deleteByPrimaryKey(sessionId);

        //判断返回值
        if (i==0){
         return new RestResult(ResultEnum.DEL_ERROR);
        }

        return new RestResult(ResultEnum.DEL_SUCCESS);

    }


    /**
     * @Author   lyw
     * @Description 修改场次信息
     * @Date 15:58 2021/7/23
     * @Param
     * @return
     */
    @Override
    public RestResult editSession(EditSessionBO bo) {

        //转换参数
        SmallMiSeckillSession smallMiSeckillSession = BeanCopierUtil.copyObj(bo, SmallMiSeckillSession.class);

        //修改时间，版本号
        smallMiSeckillSession.setModifyDate(new Date());

        //获取version
        SmallMiSeckillSession smallMiSeckillSession1 = seckillSessionMapper.selectByPrimaryKey(bo.getSessionId());
        Integer oldVersion = new Integer(smallMiSeckillSession1.getVersion());
        Integer newVersion=(oldVersion + 1);

        smallMiSeckillSession.setVersion(newVersion.toString());

        //修改
        int i = seckillSessionMapper.updateByPrimaryKeySelective(smallMiSeckillSession);

        //判断返回值
        if (i==0){
            return new RestResult(ResultEnum.EDIT_ERROR);
        }

        return new RestResult(ResultEnum.EDIT_SUCCESS);

    }


    /**
     * @Author   lyw
     * @Description 添加场次信息
     * @Date 16:20 2021/7/23
     * @Param
     * @return
     */
    @Override
    public RestResult addSession(AddSessionBO bo) {

        //转换参数
        SmallMiSeckillSession smallMiSeckillSession = BeanCopierUtil.copyObj(bo, SmallMiSeckillSession.class);

        //校验数据
        //自动生成id

        ObjectIdGenerator objectIdGenerator = new ObjectIdGenerator();
        String next = objectIdGenerator.next();
        smallMiSeckillSession.setSessionId(next);

        //秒杀商品数量限制大于0
        if (smallMiSeckillSession.getSeckillAstrict()<=0){
            return new RestResult(ResultEnum.ADD_ERROR);
        }

        //校验开始时间
        Date startTime = smallMiSeckillSession.getStartTime();
        boolean before = startTime.before(new Date());
        //如果开始时间在当前时间之前
        if (before){
            return new RestResult(ResultEnum.ADD_ERROR);
        }

        //校验结束时间
        Date endTime = smallMiSeckillSession.getEndTime();
        boolean before1 = endTime.before(startTime);
        //如果结束时间在开始时间之前
        if (before1){
            return new RestResult(ResultEnum.ADD_ERROR);
        }

        //默认值
        smallMiSeckillSession.setSessionState("seckillNotStart");
        smallMiSeckillSession.setVersion("1");
        smallMiSeckillSession.setCreationDate(new Date());
        smallMiSeckillSession.setModifyDate(new Date());

        //更新人和创建人


        //添加
        int i = seckillSessionMapper.insertSelective(smallMiSeckillSession);

        //判断返回值
        if (i==0){
            return new RestResult(ResultEnum.ADD_ERROR);
        }

        return new RestResult(ResultEnum.ADD_SUCCESS);

    }

}
